/*
extern unsigned char Digital-Input[MAX_BYTE_INPUT_IO];
extern unsigned char Digital-Output[MAX_BYTE_OUTPUT_IO];
*/
// SUL PORT A SONO  CONFIGURATI I SEGUENTI BIT DI CONTROLLO DELLO SHIFT REGISTER:
//      PA4: CLEAR                PORTA
//      PA3: LOAD                 PORTA 
//      PA2: SRCK (CLOCK BIT)     PORTA
//      PA1: LATCH (BYTE)         PORTE
//      PB0: DATA IN              PORTB

#include <avr/io.h>
#include "sr_input.h"

extern unsigned char Digital_Input[MAX_BYTE_INPUT_IO];

#define SR_INPUT_PORT		PORTB
#define SR_INPUT_LOAD		PB0
#define SR_INPUT_CLK		PB1
#define SR_INPUT_RCLK_IN	PB4
#define SR_INPUT_CS			PB6

#define SR_INPUT_READ_PORT	PINB
#define SR_INPUT_READ	    PB3


//*********************************************************************************
//                          SR_Output_Init
//*********************************************************************************
void SR_Input_Init (void){
  
 
  SR_INPUT_PORT|=(1<<SR_INPUT_LOAD);  // LOAD = 1
  SR_INPUT_PORT&=~(1<<SR_INPUT_CLK);  // bit clock = 0
  SR_INPUT_PORT&=~(1<<SR_INPUT_LOAD); //Latch = 0
  
  DI_Init();
  //SR_Input_Read ();
}

//*********************************************************************************
//                         SR_Input_Mean 
//*********************************************************************************

void SR_Input_Mean (void){

  unsigned char i,Byte_count,Bit_count,index;
  unsigned char  count_ones=0;

  for (i=0;i<SR_NUM_TO_MEAN;i++) {

     SR_Input_Read (&SR_Digital_Input[i]);
     SR_Input_wait();
  }

  for (Byte_count=0;Byte_count<MAX_BYTE_INPUT_IO;Byte_count++) {
    for (Bit_count=0;Bit_count<8;Bit_count++) {
      for (index=0;index<SR_NUM_TO_MEAN;index++) {

        if (SR_Digital_Input[index].Digital_In[Byte_count]&(1<<Bit_count)) count_ones++;

      }
      if (count_ones>SR_NUM_TO_REACH) Digital_Input[Byte_count]|=(1<<Bit_count);
      else {
            Digital_Input[Byte_count]&=~(1<<Bit_count);
      }
      count_ones=0;
    }
  }
SR_Input_Adjust(); // Inverte i segnali di ingresso secondo la maschera impostata

}

//*********************************************************************************
//                          SR_Input_read
//*********************************************************************************
//PB0: DATA IN              PORTB
void SR_Input_Read (SR_Digital_Input_type *SR_struct_Pointer){
  unsigned char volatile Byte_count,Bit_count,temp=0;
  
  DI_Reset();
  SR_Input_wait();
  SR_Input_Latch();
  SR_Input_wait();
  SR_INPUT_PORT&=~(1<<SR_INPUT_CS);
  
  for (Byte_count=0;Byte_count<MAX_BYTE_INPUT_IO;Byte_count++){
    //temp=SR_struct_Pointer->Digital_In[Byte_count];
    for (Bit_count=0;Bit_count<8;Bit_count++){
        
        if (((Byte_count==0)&&(Bit_count==0))){
           SR_Input_Load ();
           SR_Input_wait();
           //temp|=((PINB>>PB0)&0x01)<<Bit_count; 
		   temp|=((SR_INPUT_READ_PORT>>SR_INPUT_READ)&0x01)<<Bit_count;
		   
	   //Digital_Input[Byte_count]|=((PINB>>PB0)&0x01)<<Bit_count;
        }
        else {
          SR_Input_wait();
          SR_INPUT_PORT|=(1<<SR_INPUT_CLK); // bit clock = 1
          SR_Input_wait();
          temp|=((SR_INPUT_READ_PORT>>SR_INPUT_READ)&0x01)<<Bit_count;
          //Digital_Input[Byte_count]|=((PINB>>PB0)&0x01)<<Bit_count;
          SR_INPUT_PORT&=~(1<<SR_INPUT_CLK); // bit clock = 0
        }
        
    }
    SR_struct_Pointer->Digital_In[Byte_count]=temp;
    temp=0;
  
  }
  
  SR_INPUT_PORT|=(1<<SR_INPUT_CS);
  
  for (Byte_count=0;Byte_count<MAX_BYTE_INPUT_IO;Byte_count++) SR_struct_Pointer->Digital_In[Byte_count]=ByteSwap(SR_struct_Pointer->Digital_In[Byte_count]);
  //Digital_Input[Byte_count]=ByteSwap(Digital_Input[Byte_count]);
  SR_Input_wait();
  //Byte_count|=(1<<PA0);
  
}



//*********************************************************************************
//                         SR_Input_Load 
//*********************************************************************************
void SR_Input_Load (void){
  SR_INPUT_PORT&=~(1<<SR_INPUT_LOAD); //Latch =0
  SR_Input_wait();
  SR_INPUT_PORT|=(1<<SR_INPUT_LOAD);  //Latch =1
}


//*********************************************************************************
//                        SR_Input_Latch
//*********************************************************************************
void SR_Input_Latch (void){
  SR_INPUT_PORT|=(1<<SR_INPUT_RCLK_IN);  //Latch =1
  SR_Input_wait();
  SR_INPUT_PORT&=~(1<<SR_INPUT_RCLK_IN); //Latch =0
}

//*********************************************************************************
//                        SR_Input_wait
//*********************************************************************************
void SR_Input_wait (void){
  unsigned int volatile i;
  for (i=0;i<20;i++); // con una singola acquisizione degli ingressi era impostato a 100
  i++;
}

//***********************************************************************************
//                SR_Input_Adjust
//***********************************************************************************
void SR_Input_Adjust(void){
  Digital_Input[0]^=0xFF;
  Digital_Input[1]^=0xFF;
  Digital_Input[2]^=0x03;
}
